/** 
 * Copyright (C), 2010-2012, TIETIAO Software Co.,Ltd. ALL RIGHTS RESERVED. 
 * File name:BgUserAction.java
 * Version:1.0
 * Date:2011-12-28
 * Description: 后台用户管理
 * author:zhijian.zhang
 */
package com.tietiao.action.bg;

import java.io.PrintWriter;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.tietiao.action.bg.form.BgUserForm;
import com.tietiao.common.action.BaseAction;
import com.tietiao.common.exception.DataToManyException;
import com.tietiao.common.json.JsonControllerHelper;
import com.tietiao.common.util.StringUtil;
import com.tietiao.common.util.TietiaoUtil;
import com.tietiao.dao.model.SysUser;
import com.tietiao.service.sysuser.SysUserService;

@Controller
@Scope("prototype")
@SuppressWarnings("serial")
public class BguserAction extends BaseAction {

	@Resource
	private SysUserService sysUserService;

	// 用户表单信息
	private BgUserForm userForm;

	public BgUserForm getUserForm() {
		return userForm;
	}

	public void setUserForm(BgUserForm userForm) {
		this.userForm = userForm;
	}

	/**
	 * 功能：后台系统登录
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-12-30
	 * 
	 * @return
	 */
	public String login() throws Exception {
		String msg = "{success:'(1)',msg:'(2)'}";
		PrintWriter writer = response.getWriter();
		try {
			SysUser loginUser = sysUserService.loadAdminByPetName(
					userForm.getPetName(), userForm.getPassword());
			if (null != loginUser && loginUser.getType() == 1) {
				request.getSession().setAttribute(TietiaoUtil.SESSION_USER,
						loginUser);
				writer.append(msg.replace("(1)", "true").replace("(2)", ""));
			} else {
				writer.append(msg.replace("(1)", "false").replace("(2)",
						"用户名或密码错误!"));
			}
		} catch (DataToManyException e) {
			writer.append(msg.replace("(1)", "false").replace("(2)",
					"用户名或密码错误!"));
			e.printStackTrace();
		}
		writer.flush();
		writer.close();
		return null;
	}

	/**
	 * 功能：后台登陆主页
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-12-30
	 * 
	 * @return
	 */
	public String loginIndex() {
		return dispatcher("/jsp/bg/login.jsp");
	}

	/**
	 * 
	 * 功能： 跳转到用户列表页面
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-12-30
	 * 
	 * @return
	 */
	public String userIndex() {
		return dispatcher("/jsp/bg/user/user_list.jsp");
	}

	/**
	 * 
	 * 功能：加载用户列表
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-12-30
	 * 
	 * @return
	 */
	public String loadUserList() throws Exception {
		JsonControllerHelper.writePageModelToResponse(response, sysUserService
				.getUserPageModel(userForm, getStart(), getLimit(), getSort(),
						getDir()));
		return null;
	}

	/**
	 * 
	 * 功能：更新用户信息
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2012-1-2
	 * 
	 * @return
	 */
	public String updateStates() {
		String password = null;
		if (userForm.getResetPassword() != null
				&& 1 == userForm.getResetPassword()) {
			password = "abcd1234";
		}
		sysUserService.batchUpTypeOrActives(userForm.getType(),
				userForm.getIsActive(), password,
				StringUtil.wrapSqlInCondition(userForm.getUserIds(), false));
		return null;
	}
}
